package cn.sunlelin.educationserver.controller;

import cn.sunlelin.educationserver.entity.Lesson;
import cn.sunlelin.educationserver.service.CourseService;
import cn.sunlelin.educationserver.service.LessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class LessonController {
    @Autowired
    private LessonService lessonService;
    @Autowired
    private CourseService courseService;

    @PostMapping("/user/lesson/createLesson")
    public Map<String,Object> createLesson(@RequestBody Lesson lesson,
                                           @RequestHeader("userId") Integer userId){
        Map<String,Object> map=new HashMap<>();
        Map<String, Object> tCourse = courseService.selectCourseById(lesson.getCourseId());
        if (!tCourse.get("userId").equals(userId)) {
            map.put("code", 500);
            map.put("msg", "添加课时失败，你没有该权限");
            map.put("dataobject", null);
        } else {
            if (lessonService.createLesson(lesson)) {
                map.put("code", 200);
                map.put("msg", "添加课时成功");
                map.put("dataobject", null);
            } else {
                map.put("code", 500);
                map.put("msg", "添加课时失败");
                map.put("dataobject", null);
            }
        }
        return map;
    }

    @PostMapping("/user/lesson/updateLesson")
    public Map<String,Object> updateLesson(@RequestBody Lesson lesson,
                                           @RequestHeader("userId") Integer userId){
        Map<String,Object> map=new HashMap<>();
        Map<String, Object> tCourse = courseService.selectCourseById(lesson.getCourseId());
        if (!tCourse.get("userId").equals(userId)) {
            map.put("code", 500);
            map.put("msg", "修改课时失败，你没有该权限");
            map.put("dataobject", null);
        } else {
            if (lessonService.updateLesson(lesson)) {
                map.put("code", 200);
                map.put("msg", "修改课时成功");
                map.put("dataobject", null);
            } else {
                map.put("code", 500);
                map.put("msg", "修改课时失败，该课时不存在");
                map.put("dataobject", null);
            }
        }
        return map;
    }

    @GetMapping("/lesson/selectLesson/{lessonId}")
    public Map<String,Object> selectLesson(@PathVariable Integer lessonId){
        Lesson lesson=lessonService.selectLessonById(lessonId);
        Map<String,Object> map=new HashMap<>();
        if(lesson!=null){
            map.put("code",200);
            map.put("msg","根据Id查询课时成功");
            map.put("dataobject",lesson);
        }
        else {
            map.put("code",500);
            map.put("msg","根据Id查询课时失败，该Id不存在");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/lesson/selectLessonByCourseId/{courseId}/{userId}")
    public Map<String,Object> selectLessonByCourseId(@PathVariable Integer courseId,
                                                     @PathVariable Integer userId){
        List<Map<String,Object>> lessonList=lessonService.seleteLessonByCourseId(courseId,userId);
        Map<String,Object> map=new HashMap<>();
        if(lessonList.size()!=0){
            map.put("code",200);
            map.put("msg","根据课程Id查询课时成功");
            map.put("dataobject",lessonList);
        }
        else {
            map.put("code",500);
            map.put("msg","根据课程Id查询课时失败，该Id不存在");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/lesson/seleteLessonByCourseIdUpdateTimeDesc/{courseId}")
    public Map<String,Object> seleteLessonByCourseIdUpdateTimeDesc(@PathVariable Integer courseId){
        List<Map<String,Object>> lessonList=lessonService.seleteLessonByCourseIdUpdateTimeDesc(courseId);
        Map<String,Object> map=new HashMap<>();
        if(lessonList.size()!=0){
            map.put("code",200);
            map.put("msg","根据课程Id查询最近更新课时成功");
            map.put("dataobject",lessonList);
        }
        else {
            map.put("code",500);
            map.put("msg","根据课程Id查询最近更新课时失败，该Id不存在");
            map.put("dataobject",null);
        }
        return map;
    }

    @GetMapping("/user/lesson/deleteLesson/{lessonId}")
    public Map<String,Object> deleteLesson(@PathVariable Integer lessonId,
                                           @RequestHeader("userId") Integer userId){
        Map<String,Object> map=new HashMap<>();
        Lesson tLesson=lessonService.selectLessonById(lessonId);
        Map<String, Object> tCourse = courseService.selectCourseById(tLesson.getCourseId());
        if (!tCourse.get("userId").equals(userId)) {
            map.put("code", 500);
            map.put("msg", "删除课时失败，你没有该权限");
            map.put("dataobject", null);
        } else {
            if (lessonService.deleteLesson(lessonId)) {
                map.put("code", 200);
                map.put("msg", "删除课时成功");
                map.put("dataobject", null);
            } else {
                map.put("code", 500);
                map.put("msg", "删除课时失败，该课时不存在");
                map.put("dataobject", null);
            }
        }
        return map;
    }
}
